package com.yang.library.service.service.impl;

import com.chen.librarysystem.log.LibraryLog;
import com.yang.library.bookenum.State;
import com.yang.library.entry.User;
import com.yang.library.service.mapper.FileMapper;
import com.yang.library.service.mapper.impl.FileMapperImpl;
import com.yang.library.service.service.UserService;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import static com.yang.library.bookenum.State.SUCCESS;

public class UserServiceImpl implements UserService {
    private final FileMapper fileMapper = new FileMapperImpl();

    public UserServiceImpl() {
    }

    @Override
    public boolean isRegistered(String username, String password) {
        Map<String, User> users = fileMapper.loadUserData();
        if (users == null || users.isEmpty()) {
            return false;
        }
        // 获取用户数据
        if (users.containsKey(username)) {
            // 验证密码
            return users.get(username).getPassword().equals(password);
        }
        return false;
    }

    @Override
    public boolean register(User user) {
        Map<String, User> users = new HashMap<>(fileMapper.loadUserData());
        users.put(user.getName(), user);
        return fileMapper.saveUserData(users);
    }

    @Override
    public Map<State, User> login(String username, String password) {
        Map<String, User> users = fileMapper.loadUserData();
        if (users == null) {
            return null;
        }
        User user = users.get(username);
        if (user == null || !user.getPassword().equals(password)) {
            return null;
        }
        return Map.of(SUCCESS, user);
    }

    @Override
    public boolean changePassword(String username, String oldPassword, String newPassword) {
        Map<String, User> users = fileMapper.loadUserData();
        if (username == null || oldPassword == null || newPassword == null) return false;
        if (username.isEmpty() || oldPassword.isEmpty() || newPassword.isEmpty()) return false;
        if (users.containsKey(username)) {
            User user = users.get(username);
            if (user.getPassword().equals(oldPassword)) {
                user.setPassword(newPassword);
                users.put(username, user);
                return fileMapper.saveUserData(users);
            }
            return false;
        }
        return false;
    }

    @Override
    public boolean updateUser(String username, User user) {
        Map<String, User> users = fileMapper.loadUserData();
        if (username == null || user == null || username.isEmpty()) return false;
        if (users.containsKey(username)) {
            users.put(username, user);
            return fileMapper.saveUserData(users);
        }
        return false;
    }

    @Override
    public boolean deleteUser(String username) {
        Map<String, User> users = fileMapper.loadUserData();
        if (username == null || username.isEmpty()) return false;
        if (users.remove(username) == null) return false;
        return fileMapper.saveUserData(users);
    }

    @Override
    public List<User> getAllUsers() {
        return fileMapper.loadUserData().values().stream().toList();
    }

    @Override
    public User getUserByName(String username) {
        return fileMapper.loadUserData().get(username);
    }
}
